<?php
/**
 * $Id:$
 *
 * FGS is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 


class fgsCommands {

	/** 
	 * get() return all commands
	 */
	function get($os = null, $gid = null, $hide_deleted = false) {

		$where = array();
		if (!is_null($os))
			$where[] = sprintf('os = "%s"', fgsSql::escapeStr($os));
		if (!is_null($gid))
			$where[] = sprintf('gid = "%s"', fgsSql::escapeStr($gid));
		if ($hide_deleted)
			$where[] = 'NOT is_deleted';

		$query = sprintf('
			SELECT *
			FROM fgs_commands 
			%s
			ORDER BY is_deleted, name',
			count($where) ? 'WHERE '.implode(' AND ', $where) : ''
		);
				
		$sql = fgs::sql();
		$sql->query($query);
		return $sql->fetchAll();
	}

	/**
	 * getById() return a game by id
	 */
	function getById($id) {
		$query = sprintf('SELECT * FROM fgs_commands WHERE id=%d', $id);
		$sql = fgs::sql();
		$sql->query($query);
		if (!$sql->nbrows())
			return false;
		return $sql->getline();
	}

	/**
	 * add() add a game command into database
	 */
	function add($gid, $name, $bin_name, $version, $os, $command, $help, $is_disabled, $is_master = 0) {
		$query = sprintf('
			INSERT INTO fgs_commands
			SET 
				gid = "%s",
				name = "%s",
				bin_name = "%s",
				version = "%s",
				os = "%s",
				command = "%s",
				help = "%s",
				is_deleted = %d,
				is_master = %d,
				dt_lastupdate = NOW()',
			fgsSql::escapeStr($gid),
			fgsSql::escapeStr($name),
			fgsSql::escapeStr($bin_name),
			fgsSql::escapeStr($version),
			fgsSql::escapeStr($os),
			fgsSql::escapeStr($command),
			fgsSql::escapeStr($help),
			$is_disabled ? 1 : 0,
			$is_master ? 1 : 0
		);

		$sql = fgs::sql();
		return $sql->query($query) ? true : false;
	}

	/**
	 * update() update a game command into database
	 */
	function update($id, $gid, $name, $bin_name, $version, $os, $command, $help, $is_deleted = 0, $is_master = 0) {
		$query = sprintf('
			UPDATE fgs_commands
			SET 
				gid = "%s",
				name = "%s",
				bin_name = "%s",
				version = "%s",
				os = "%s",
				command = "%s",
				help = "%s",
				is_deleted = %d,
				is_master = %d,
				dt_lastupdate = NOW()
			WHERE id = %d',
			fgsSql::escapeStr($gid),
			fgsSql::escapeStr($name),
			fgsSql::escapeStr($bin_name),
			fgsSql::escapeStr($version),
			fgsSql::escapeStr($os),
			fgsSql::escapeStr($command),
			fgsSql::escapeStr($help),
			$is_deleted ? 1 : 0,
			$is_master ? 1 : 0,
			$id
		);

		$sql = fgs::sql();
		return $sql->query($query) ? true : false;
	}

	/**
	 * enable() enable a game command
	 */
	function enable($id, $enable) {
		$query = sprintf('
			UPDATE fgs_commands
			SET 
				is_deleted = %d
			WHERE
				id = %d',
			$enable ? 0 : 1,
			$id
		);

		$sql = fgs::sql();
		return $sql->query($query) ? true : false;
	}
	
	/**
	 * retrieve() retrieve the command list from
	 * master (freegs.net)
	 */
	function retrieve() {
		
		/* Retrieve the command list from the master server via XMLRPC */
    $xml = new IXR_Client(fgs::config('url_master_server'));
    $xml->query('games.list');
	
		if ($xml->isError()) {
			echo $xml->getErrorMessage();
			return false;
		}
		
		$list = $xml->getResponse();
		
		/* Delete the old master commands */
		$query = 'DELETE FROM fgs_commands
		  WHERE is_master = 1';
		$sql = fgs::sql();
		$sql->query($query);

		foreach ($list as $game) {
			fgsCommands::add($game['game_gid'], $game['game_name'], $game['game_bin_name'], $game['game_version'], $game['game_os'], $game['game_command'], $game['game_help'], 0, 1);
		}
		
		return true;
	}
	

}

?>
